home *** CD-ROM | disk | FTP | other *** search
/ Ham Radio 2000 #1 / Ham Radio 2000.iso / ham2000 / tcp_ip / wnos / wn941101 / ax25dump.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-10-07  |  5.1 KB  |  212 lines

  1. #include <stdio.h>
  2. #include <conio.h>
  3. #include "global.h"
  4. #include "config.h"
  5. #ifdef AX25
  6. #include "mbuf.h"
  7. #include "ax25.h"
  8. #include "timer.h"
  9. #include "trace.h"
  10. #include "socket.h"
  11.  
  12. static char * near decode_type(int type) {
  13.    switch(type){
  14.      case I:
  15.        return "I";
  16.      case SABM:
  17.        return "SABM";
  18.      case DISC:
  19.        return "DISC";
  20.      case DM:
  21.        return "DM";
  22.      case UA:
  23.        return "UA";
  24.      case RR:
  25.        return "RR";
  26.      case RNR:
  27.        return "RNR";
  28.      case REJ:
  29.        return "REJ";
  30.      case FRMR:
  31.        return "FRMR";
  32.      case UI:
  33.        return "UI";
  34.      default:
  35.        return "[invalid]";
  36.    }
  37. }
  38.  
  39. /* Figure out the frame type from the control field
  40.  * This is done by masking out any sequence numbers and the
  41.  * poll/final bit after determining the general class (I/S/U) of the frame
  42.  */
  43. static int near ftype(int control)
  44. {
  45.   if((control & 1) == 0)  /* An I-frame is an I-frame... */
  46.     return I;
  47.   if(control & 2)         /* U-frames use all except P/F bit for type */
  48.     return (control & ~PF);
  49.   else                    /* S-frames use low order 4 bits for type */
  50.     return (control & 0x0f);
  51. }
  52.  
  53. /* Dump an AX.25 packet header */
  54. void ax25_dump(FILE *fp,struct mbuf **bpp,int check)
  55. {
  56.   char tmp[AXBUF], tmp1[AXBUF], frmr[3], cp[2 * AXALEN];
  57.   int control, pid, seg, ipcam, cmdrsp, type, dama_master, unsegmented = 0;
  58.   struct mbuf *bp;
  59.  
  60.   trprintf(fp,"AX25: ");
  61.  
  62.   if(pullup(bpp,cp,2 * AXALEN) == (2 * AXALEN)) {
  63.     dama_master = DAMA - (cp[ALEN + AXALEN] & DAMA);
  64.     trprintf(fp,"%s->%s ",pax25(tmp,&cp[AXALEN]),pax25(tmp1,cp));
  65.  
  66.     if(((cp[ALEN]) & C) == (cp[ALEN + AXALEN] & C))
  67.       cmdrsp = VERS1;
  68.     else
  69.       cmdrsp = (cp[ALEN] & C) ? DST_C : SRC_C;
  70.  
  71.     if((cp[ALEN + AXALEN] & E) == 0) {
  72.       trprintf(fp,"v ");
  73.       while((cp[ALEN] & E) == 0) {
  74.     if(pullup(bpp,cp,AXALEN) == AXALEN)
  75.       trprintf(fp,"%s%s ",pax25(tmp,cp),(cp[ALEN] & REPEATED) ? "*" : "");
  76.     else
  77.       goto err;
  78.       }
  79.     }
  80.   } else {
  81.     goto err;
  82.   }
  83.  
  84.   if((control = PULLCHAR(bpp)) == -1) {
  85. err:
  86.     trprintf(fp,"bad header\n");
  87.     return;
  88.   }
  89.  
  90.   trprintf(fp,"%s",decode_type((type = ftype(control))));
  91.  
  92.   if(control & PF) {
  93.     switch(cmdrsp) {
  94.       case CMD:
  95.     trprintf(fp,"(P)");
  96.     break;
  97.       case RESP:
  98.     trprintf(fp,"(F)");
  99.     break;
  100.       default:
  101.     trprintf(fp,"(P/F)");
  102.     break;
  103.     }
  104.   }
  105.  
  106.   /* Dump sequence numbers */
  107.   if((type & 0x3) != U)               /* I or S frame? */
  108.     trprintf(fp," NR=%d",(control>>5)&7);
  109.   if(type == I || type == UI){
  110.     if(type == I)
  111.       trprintf(fp," NS=%d",(control>>1)&7);
  112.     /* Decode I field */
  113.     if((pid = PULLCHAR(bpp)) != -1){        /* Get pid */
  114.       if(pid == PID_SEGMENT){
  115.     seg = PULLCHAR(bpp);
  116.     trprintf(fp,"%s remain %u",seg & SEG_FIRST ? " First seg;" : "",seg & SEG_REM);
  117.     if(seg & SEG_FIRST)
  118.       pid = PULLCHAR(bpp);
  119.       } else
  120.     unsegmented = 1;
  121.  
  122. /* IPCAM-feature - DB3FL.910104 */
  123.       bp = *bpp;
  124.       if(pid == PID_NO_L3 && bp->cnt >= 20
  125.         && bp->data[0] == 0x45
  126.         && bp->data[1] == 0x00
  127.         && bp->data[2] < 0x02) {
  128.     pid = PID_IP;
  129.     ipcam = 1;
  130.       } else
  131.     ipcam=0;
  132. /* */
  133.  
  134.       if (dama_master)
  135.     trprintf(fp," [DAMA]");
  136.  
  137.       if(pid == PID_SEGMENT)
  138.     trprintf(fp,"\n");
  139.       else {
  140.     textattr(WHITE);
  141.     trprintf(fp," pid=");
  142.     switch(pid){
  143.       case PID_ARP:
  144.         textattr(YELLOW);
  145.         trprintf(fp,"ARP\n");
  146.         arp_dump(fp,bpp);
  147.         break;
  148.       case PID_NETROM:
  149.             textattr(BLUE);
  150.         trprintf(fp,"NET/ROM\n");
  151. #ifdef NETROM
  152.         /* Don't verify checksums unless unsegmented */
  153.         netrom_dump(fp,bpp,unsegmented);
  154. #endif
  155.         break;
  156.       case PID_IP:
  157.         textattr(YELLOW);
  158.         if(ipcam)
  159.           trprintf(fp,"IPCAM\n");
  160.         else
  161.           trprintf(fp,"IP\n");
  162.         /* Don't verify checksums unless unsegmented */
  163.         ip_dump(fp,bpp,unsegmented);
  164.         break;
  165.       case PID_X25:
  166.         trprintf(fp,"X.25\n");
  167.         break;
  168.       case PID_TEXNET:
  169.         trprintf(fp,"TEXNET\n");
  170.         break;
  171.       case PID_FLEXNET:
  172.         textattr(MAGENTA);
  173.         trprintf(fp,"FLEXNET\n");
  174. #ifdef FLEXNET
  175.         flexnet_dump(fp,bpp);        /* to be written */
  176. #endif
  177.         break;
  178.       case PID_NO_L3:
  179.             textattr(WHITE);
  180.         trprintf(fp,"Text\n");
  181.         break;
  182.       default:
  183.         textattr(RED);
  184.         trprintf(fp,"0x%x\n",pid);
  185.     }
  186.       }
  187.     }
  188.   } else if(type == FRMR && pullup(bpp,frmr,3) == 3){
  189.     trprintf(fp,"\nVr=%d Vs=%d",(frmr[1] >> 5) & MMASK,(frmr[1] >> 1) & MMASK);
  190.     if(frmr[2] & W)
  191.       trprintf(fp," Invalid control field");
  192.     if(frmr[2] & X)
  193.       trprintf(fp," Illegal I-field");
  194.     if(frmr[2] & Y)
  195.       trprintf(fp," Too-long I-field");
  196.     if(frmr[2] & Z)
  197.       trprintf(fp," Invalid seq number");
  198.     trprintf(fp,": %s\n",decode_type(ftype(frmr[0])));
  199.   } else
  200.     trprintf(fp," %s\n",dama_master ? "[DAMA] " : "");
  201. }
  202.  
  203.  
  204. /* Return 1 if this packet is directed to us, 0 otherwise. Note that
  205.  * this checks only the ultimate destination, not the digipeater field
  206.  */
  207. int ax_forus(struct iface *iface,struct mbuf *bp) {
  208.   return (addreq(bp->data,iface->hwaddr));
  209. }
  210.  
  211. #endif /* AX25 */
  212.